package model

import (
	"ginblog/utils/encrypt"
	"ginblog/utils/errmsg"

	"gorm.io/gorm"
)

// User 用户信息表
type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(20);not null" json:"username" validate:"required,min=4,max=12" label:"用户名"`
	Password string `gorm:"type:varchar(20);not null" json:"password" validate:"required,min=6,max=20" label:"密码"`
	Role     int    `gorm:"type:int;DEFAULT:2" json:"role" validate:"required,gte=2" label:"用户权限"`
}

// CheckUserExist 查询用户是否已存在
func CheckUserExist(username string) (code int) {
	var user User
	db.Select("id").Where("username = ?", username).First(&user)
	if user.ID > 0 {
		return errmsg.ERROR_USERNAME_USED // 1001
	}
	return errmsg.SUCCESS // 200
}

// CheckUpdateUser 跟新查询
func CheckUpdateUser(id int, username string) (code int) {
	var user User
	db.Select("id, username").Where("id = ?", id).First(&user)
	if user.ID == uint(id) {
		return errmsg.SUCCESS
	}
	if user.ID > 0 {
		return errmsg.ERROR_USERNAME_USED // 1001
	}
	return errmsg.SUCCESS // 200
}

// CreateUser 注册新增用户
func CreateUser(data *User) (code int) {
	data.Password = encrypt.EncryptPassword(data.Password)
	err := db.Create(&data).Error
	if err != nil {
		return errmsg.ERROR // 500
	}
	return errmsg.SUCCESS
}

// GetUser 查询单个用户
func GetUser(id int) (User, int) {
	var user User
	err := db.Select("id", "username", "role").Where("ID = ?", id).First(&user).Error
	if err != nil {
		return user, errmsg.ERROR
	}
	return user, errmsg.SUCCESS
}

// BeforeSave 在User结构体中，gorm自动调用钩子函数,创建用户时加密密码。
// func (u *User) BeforeSave(tx *gorm.DB) (err error) {
// 	u.Password = encrypt.EncryptPassword(u.Password)
// 	return
// }

// GetUsers 查询用户列表数据
func GetUsers(username string, pageSize int, pageNum int) ([]User, int64) {
	var users []User
	var total int64
	if username != "" {
		db.Select("id", "username", "role").Where("username LIKE ?", username+"%").Find(&users).Count(&total).Limit(pageSize).Offset((pageNum - 1) * pageSize)
		return users, total
	}

	db.Select("id", "username", "role").Find(&users).Count(&total).Limit(pageSize).Offset((pageNum - 1) * pageSize)
	if err == gorm.ErrRecordNotFound {
		return nil, 0
	}
	return users, total
}

// DeleteUser 删除用户的方法
func DeleteUser(id int) int {
	err = db.Where("id = ?", id).Delete(&User{}).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}

// EditUser 编辑用户信息 只可修改密码以外的字段
func EditUser(id int, data *User) int {
	var user User
	var maps = make(map[string]interface{})
	maps["username"] = data.Username
	maps["role"] = data.Role
	err = db.Model(&user).Where("id = ?", id).Updates(maps).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCESS
}

// CheckLogin 登陆验证
func CheckLogin(username string, password string) int {
	var user User
	db.Where("username = ?", username).First(&user)
	if user.ID == 0 {
		return errmsg.ERROR_USER_NOT_EXIST
	}
	if encrypt.EncryptPassword(password) != user.Password {
		return errmsg.ERROR_PASSWORD_WRONG
	}
	if user.Role != 1 {
		return errmsg.ERROR_USER_NOT_RIGHT
	}
	return errmsg.SUCCESS
}
